package cn.onlineclass.common;
import cn.onlineclass.entity.OUser;
import cn.onlineclass.service.OUserService;
import cn.onlineclass.util.StringUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class UserController {

    //显示所有的用户
    public AjaxResult search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"), 1);
        String name = request.getParameter("name");
        String wechat = request.getParameter("wechat");
        request.setAttribute("name", StringUtil.trim(name));
        request.setAttribute("wechat", StringUtil.trim(wechat));
        //Page<Product> p = ProductService.TYPE.search(pageNo, Page.PAGE_SIZE, pname, style);
        AjaxPage p = OUserService.TYPE.search(pageNo, 10, name, wechat);
        String s2 = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(s2);
        return null;
    }

    //增加新的用户
    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String name = request.getParameter("name");
        String wechat = request.getParameter("wechat");
        String tele = request.getParameter("tele");
        String openid = request.getParameter("openid");
        Date createtime = StringUtil.parse(request.getParameter("createtime"));
        //Date modifytime = StringUtil.parse(request.getParameter("modifytime"));

        OUser u = new OUser(name, wechat, tele, openid, createtime, null);
        AjaxResult add = OUserService.TYPE.add(u);
        String s = JSON.toJSONString(add);
        response.getWriter().print(s);
        return add;

    }
    //修改用户信息
    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String wechat = request.getParameter("wechat");
        String tele = request.getParameter("tele");
        String openid = request.getParameter("openid");
//        Date createtime = StringUtil.parse(request.getParameter("createtime"));
//        Date modifytime = StringUtil.parse(request.getParameter("modifytime"));


        OUser u = new OUser(id,name, wechat, tele, openid);


        AjaxResult update = OUserService.TYPE.update(u);
        String s = JSON.toJSONString(update);
        response.getWriter().print(s);
        return update;
    }


    //删除用户
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        AjaxResult del = OUserService.TYPE.del(id);
        String s = JSON.toJSONString(del);
        response.getWriter().print(s);
        return del;
    }
    public void get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toLong(request.getParameter("id"));
        AjaxResult ajaxResult = OUserService.TYPE.get(id);
        String s = JSON.toJSONString(ajaxResult);
        response.getWriter().print(s);
    }
}
